<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SwitchController
 *
 * @author Admin
 */
class Planning_HouseController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function preDispatch() {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('/user/login');
        }
        $view = new Zend_View();
        $view->headTitle(' > Planning > Hosue');
        $this->view->userDetail = $auth->getIdentity();
    }

    public function indexAction() {
        $houseModel = new Application_Model_HouseModel();
        $paginator = $houseModel->getAllHouses($this->_request);
        $this->view->paginator = $paginator;
    }

    public function listAction() {
        $this->_helper->layout->disableLayout();
        $houseModel = new Application_Model_HouseModel();
        $paginator = $houseModel->getAllHouses($this->_request);
        $this->view->paginator = $paginator;
    }

    public function addAction() {
        $this->_helper->layout->disableLayout();
        $houseModel = new Application_Model_HouseModel();
        //create form
        $form = new Application_Form_house_AddForm();

        // if request is post
        if ($this->_request->isPost()) {

            // if form is valid

            if ($form->isValid($this->_request->getPost())) {

                $data = array(
                    'official_address' => $this->_request->getPost('officialAddress'),
                    'address' => $this->_request->getPost('address'),
                    'postnr' => $this->_request->getPost('postnr'),
                    'postadress' => $this->_request->getPost('postadress'),
                    'owner_name' => $this->_request->getPost('ownerName'),
                    'customer_code' => $this->_request->getPost('customerCode'),
                    'registration_date' => $this->_request->getPost('regDate'),
                    'seller' => $this->_request->getPost('seller'),
                    'personnr' => $this->_request->getPost('personalNo'),
                    'tel1' => $this->_request->getPost('tel1'),
                    'tel2' => $this->_request->getPost('tel2'),
                    'email' => $this->_request->getPost('email'),
                    'installation_avropad' => $this->_request->getPost('avropad'),
                    'blast_klar' => $this->_request->getPost('blastKlar'),
                    'aviserad_vecka' => $this->_request->getPost('aviseradVecka'),
                    'install_klar' => $this->_request->getPost('installKlar'),
                    'installerad_av' => $this->_request->getPost('installeradAv'),
                    'cabinet' => $this->_request->getPost('cabinetList'),
                    'switch' => $this->_request->getPost('switchList'),
                    'status' => $this->_request->getPost('status')
                );
                $houseModel->save($data);

                //      $this->_redirect("planning/house");
                //      exit;
            }
        }

        // assign form variable to view
        $this->view->form = $form;
    }

    public function editAction() {
        $this->_helper->layout->disableLayout();
        $houseModel = new Application_Model_HouseModel();

        $houseId = $this->_request->getParam('id');
        $houseDetail = $houseModel->getHouse('id', $houseId);
        //create form
        $form = new Application_Form_house_AddForm();

        $form->populate(array(
            'officialAddress' => $houseDetail['official_address'],
            'address' => $houseDetail['address'],
            'postnr' => $houseDetail['postnr'],
            'postadress' => $houseDetail['postadress'],
            'ownerName' => $houseDetail['owner_name'],
            'customerCode' => $houseDetail['customer_code'],
            'cabinetList' => $houseDetail['cabinet'],
            'switchList' => $houseDetail['switch'],
            'status' => $houseDetail['status'],
            'regDate' => $houseDetail['registration_date'],
            'seller' => $houseDetail['seller'],
            'personalNo' => $houseDetail['personnr'],
            'tel1' => $houseDetail['tel1'],
            'tel2' => $houseDetail['tel2'],
            'email' => $houseDetail['email'],
            'avropad' => $houseDetail['installation_avropad'],
            'blastKlar' => $houseDetail['blast_klar'],
            'installKlar' => $houseDetail['install_klar'],
            'aviseradVecka' => $houseDetail['aviserad_vecka'],
            'installeradAv' => $houseDetail['installerad_av'],
        ));
        // if request is post
        if ($this->_request->isPost()) {

            // if form is valid

            if ($form->isValid($this->_request->getPost())) {

                $data = array(
                    'id' => $houseId,
                    'official_address' => $this->_request->getPost('officialAddress'),
                    'address' => $this->_request->getPost('address'),
                    'postnr' => $this->_request->getPost('postnr'),
                    'postadress' => $this->_request->getPost('postadress'),                    
                    'owner_name' => $this->_request->getPost('ownerName'),
                    'customer_code' => $this->_request->getPost('customerCode'),
                    'registration_date' => $this->_request->getPost('regDate'),
                    'seller' => $this->_request->getPost('seller'),
                    'personnr' => $this->_request->getPost('personalNo'),
                    'tel1' => $this->_request->getPost('tel1'),
                    'tel2' => $this->_request->getPost('tel2'),
                    'email' => $this->_request->getPost('email'),
                    'installation_avropad' => $this->_request->getPost('avropad'),
                    'blast_klar' => $this->_request->getPost('blastKlar'),
                    'aviserad_vecka' => $this->_request->getPost('aviseradVecka'),
                    'install_klar' => $this->_request->getPost('installKlar'),
                    'installerad_av' => $this->_request->getPost('installeradAv'),
                    'cabinet' => $this->_request->getPost('cabinetList'),
                    'switch' => $this->_request->getPost('switchList'),
                    'status' => $this->_request->getPost('status')
                );
                $houseModel->save($data);

                //      $this->_redirect("planning/house");
                //      exit;
            }
        }

        // assign form variable to view
        $this->view->form = $form;
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $houseId = $this->_request->getParam('id');
        $houseModel = new Application_Model_HouseModel();

        $houseModel->delete("id = '$houseId'");
        //       $this->_redirect('planning/house');
    }

    public function housedetailAction() {
        $this->_helper->layout->disableLayout();

        $houseId = $this->_request->getParam('id');
        $page = $this->_request->getParam('page');
        $houseModel = new Application_Model_HouseModel();

        $houseDetail = $houseModel->getHouse('id', $houseId);
        $this->view->houseDetail = $houseDetail;
        $this->view->paginator = $houseModel->getHouseRemarks($houseId, $page);
    }

    public function remarksAction() {
        $this->_helper->layout->disableLayout();
        $houseId = $this->_request->getParam('id');
        $page = $this->_request->getParam('page');
        $houseModel = new Application_Model_HouseModel();
        $this->view->paginator = $houseModel->getHouseRemarks($houseId, $page);
    }

    public function addremarksAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $houseId = $this->_request->getParam('id');
        $houseModel = new Application_Model_HouseModel();

        if ($this->_request->isPost()) {
            $auth = Zend_Auth::getInstance();
            $data = array(
                'house_id' => $houseId,
                'user_id' => $auth->getIdentity()->id,
                'remark' => $this->_request->getPost('remarks'),
                'posted_at' => Zend_Date::now()->toString('yyyy-MM-dd HH:mm:ss')
            );
            $houseModel->saveRemarks($data);
        }
    }

}